package com.xinjiang.electricpower.timer;

import com.xinjiang.electricpower.domain.DTO.KettlePullDataTaskVo;
import com.xinjiang.electricpower.service.IMarketPullDataTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhanghui
 * @date 2023/2/14 10:25
 * @Description 营销定时拉取数据任务
 */
@Slf4j
@Service
public class MarketPullDataTask implements IMarketPullDataTask {

    @Override
    public void runTaskParam(String startDate, String endDate, String businessType, String fNamePath) {
        try {
            InputStream in = this.getClass().getClassLoader().getResourceAsStream(fNamePath);
            Map<String, String> initKettleParam = new HashMap<>();
            initKettleParam.put("startDate", startDate);
            initKettleParam.put("endDate", endDate);
            //执行对应ktr 文件转换
            KettleQuartz.runKettleTransfer(null, businessType, in);
        }catch (Exception e){
            log.error("拉取{}工单数据异常 {}",businessType,e);
        }

    }
    @Override
    public void runTaskParam(String orgNo, String businessType, String fNamePath) {
        try {
            InputStream in = this.getClass().getClassLoader().getResourceAsStream(fNamePath);
            Map<String, String> initKettleParam = new HashMap<>();
            initKettleParam.put("orgNo", orgNo);
            //执行对应ktr 文件转换
            KettleQuartz.runKettleTransfer(initKettleParam, businessType, in);
        }catch (Exception e){
            log.error("拉取{}工单数据异常 {}",businessType,e);
        }

    }

    @Override
    @Async()
    public void runMarketTaskParam(KettlePullDataTaskVo vo) {
        Instant start = Instant.now();
        log.info("拉取{}工单数据开始：{}",KettleQuartz.YIN_XIAO, start);
        //执行拉取营销数据对应的ktr
//        runTaskParam(vo.getOrgNo(),KettleQuartz.YIN_XIAO,"kettle/yx_all_order_general.ktr");
        runTaskParam(vo.getOrgNo(),KettleQuartz.YIN_XIAO,"xinzhuang/new_work_order.ktr");
        Instant end = Instant.now();
        log.info("拉取{}工单数据结束：{}",KettleQuartz.YIN_XIAO, end);
        log.info("拉取{}工单数据，耗时：{} 毫秒",KettleQuartz.YIN_XIAO, Duration.between(start,end).toMillis());
    }

}
